﻿using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.Currency.FileUtils;
using SpiderStudy.Utils.Helper;
using System;
using System.Collections.Generic;

namespace SpiderStudy.WebSite.Spider360
{
    internal class ExcavateKeys
    {    /// <summary>
         /// 递归查询数据，挖掘关键词
         /// </summary>
         /// <param name="Keys">关键词</param>
         /// <param name="RecursionCount">递归次数</param>
         /// <returns></returns>
        public void SaveTitleList(string Keys, int RecursionCount)
        {
            List<string> TempList = RecurSionKeys(Keys, RecursionCount);
            String FloderPath = new FloderHelper().CreateFloderPath(StaticValue.TempPath, "SEOData");
            FileHelper.WriteUTF8List(FloderPath + string.Format("Key-{0}.txt", TextHelper.ReplaceBadChar(Keys)), TempList);
            new SaveHelper().DownloadComplted(FloderPath);
        }

        /// <summary>
        /// 递归查询数据，挖掘关键词
        /// </summary>
        /// <param name="Keys">关键词</param>
        /// <param name="RecursionCount">递归次数</param>
        /// <returns></returns>
        public List<string> RecurSionKeys(string Keys, int RecursionCount)
        {
            List<string> TempList = new List<string>();

            if (RecursionCount == 0)
                return TempList;
            List<string> GetKeyList = new GetKeys().ReadJson(Keys);
            TempList.AddRange(GetKeyList);
            foreach (string Line in new GetKeys().ReadJson(Keys))
            {
                int TempRecursionCount = RecursionCount;//缓存递归次数
                Console.WriteLine("递归进行，当前递归次数{0}", --TempRecursionCount);
                TempList.AddRange(RecurSionKeys(Line, TempRecursionCount));
            }

            --RecursionCount;//减少一次递归次数

            return TempList;
        }
    }
}